package base;

import java.awt.geom.Point2D;
import java.io.Serializable;


public class Turret implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 4378558408718968700L;

		// location relative to the ship 
		private double x=0;

		private double y=0;
		protected double beginAngle=0,endAngle=0;
		
		private long lastFired = 0; // time when the turret was last fired
		
		// TODO: this data should be based upon the type of the turret
		private double optimal=0;
		private int cycleDuration=0; // time in milliseconds turret needs to cool down after firing before being able to fire again
		private double baseDamage=0; 
		private double tracking=0;
		private double signatureRadius=0;
		private double falloff=0;
		
		/**
		 * 
		 * @param shipRef
		 * @param begin
		 * @param end
		 * @param x
		 * @param y
		 * @param optimal
		 * @param cycleDuration
		 * @param baseDamage
		 * @param tracking
		 * @param signatureRadius
		 * @param falloff falloff of the turret. damage model might handle falloffs of <1.0 strangely
		 */
		public Turret(Ship ship, double begin, double end, double x, double y, double optimal, 
				int cycleDuration, double baseDamage, double tracking, double signatureRadius, double falloff)
		{
			this.ship = ship;
			this.beginAngle = begin;
			this.endAngle = end;
			this.x = x;
			this.y = y;
			this.optimal = optimal;
			this.cycleDuration = cycleDuration;
			this.baseDamage = baseDamage;
			this.tracking = tracking;
			this.signatureRadius = signatureRadius;
			this.falloff = falloff;
		}
		
		private Ship ship = null;
		
		public double getBeginAngle() {
			Ship ship = getShip();
			return Geometry.ensurePositive(ship.getHeading() + beginAngle);
		}
		
		public double getEndAngle() {
			Ship ship = getShip();
			return Geometry.ensurePositive(ship.getHeading() + endAngle);
		}
		
		public Arc getArc()
		{
			Ship ship = getShip();
			return new Arc(Geometry.ensurePositive(ship.getHeading() + beginAngle), Geometry.ensurePositive(ship.getHeading() + endAngle));
		}
		
		/*
		 * getters for getting global location of the turret, i.e. taking ship location into account
		 */
		public double getX()
		{
			Ship ship = getShip();
			double sheading = ship.getHeading();
			return ship.getX() + Geometry.getX(sheading) * x - Geometry.getY(sheading) * y;
		}
		
		public double getY()
		{
			Ship ship = getShip();
			double sheading = ship.getHeading();
			return ship.getY() + Geometry.getY(sheading) * y + Geometry.getX(sheading) * x;
		}
		
		public Point2D.Double getPos()
		{
			Ship ship = getShip();
			return Geometry.getPoint(ship.getHeading(), x, y, ship.getX(), ship.getY());
		}

		public double getOptimal() {
			return optimal;
		}

		public boolean isReadyToBeFired(long timeNow)
		{
			return (timeNow >= lastFired + cycleDuration);
		}

		public void setLastFired(long lastFired) {
			this.lastFired = lastFired;
		}

		public double getBaseDamage() {
			return baseDamage;
		}

		public double getTracking() {
			return tracking;
		}

		public double getSignatureRadius() {
			return signatureRadius;
		}

		public double getOptimalSq() {
			return optimal * optimal;
		}
		
		public double getFalloff() {
			return falloff;
		}
		
		public Ship getShip()
		{
			return ship;
		}	
}
